<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends CI_Controller {

	public function index()
	{
		$data['title'] = "Automatic Essay Scoring";
		
		if($this->session->userdata('is_logged_in')=="admin"){
			redirect('admin');
		}else if($this->session->userdata('is_logged_in')=="user"){
			redirect('user/test');
        }else{
			$data['title']				 = "Automatic Essay Scoring - User Login";
			$data['main_content']	 = 'user/user_login';
			$this->load->view('includes/template_login', $data);
        }
	}

	function __encrip_password($password) {
        return md5($password);
    }
	
	function validate_credentials()
	{	

		$this->load->model('model_user');

		$username = $this->input->post('username');
		$password = $this->__encrip_password($this->input->post('password'));

		$is_valid = $this->model_user->validate($username, $password);
		if($is_valid)
		{
			$data = array(
				'username' => $this->input->post('username'),
				'is_logged_in' => 'user'
			);
			$this->session->set_userdata($data);
			redirect('user/test');
		}
		else // incorrect username or password
		{
			$data['message_error'] = TRUE;
			$data['title']				 = "Automatic Essay Scoring - User Login";
			$data['main_content']	 = 'user/user_login';
			$this->load->view('includes/template_login', $data);
		}
	}	
	
	function logout()
	{
		$this->session->sess_destroy();
		redirect('user');
	}
	
}
